﻿using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Model;
using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetMVC.Service
{
    public partial class SiteNavService : BaseService<SiteNav>, ISiteNavService
    {
        public ISiteContentService siteContentService { get; set; }
        private IQueryable<SiteNav> GetJoinAll()
        {
            return GetAll()
                .Include(u => u.FunctionalModule)
                .AsNoTracking();
        }

        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <returns></returns>
        public SiteNavDTO[] GetSiteNavs()
        {
            return GetJoinAll().OrderBy(e => e.Sort).ThenBy(e => e.Id).ToList().Select(p => ToDTO(p)).ToArray();
        }

        /// <summary>
        /// 获取显示菜单
        /// </summary>
        /// <param name="navLevel">菜单级数</param>
        /// <returns></returns>
        public SiteNavDTO[] GetShowSiteNavs(int navLevel = 0)
        {
            IQueryable<SiteNav> SiteNavs = GetJoinAll().Where(e => e.IsShow == true);
            if (navLevel>0)
            {
                SiteNavs.Where(e => e.NavLevel <= navLevel);
            }
            return   SiteNavs.OrderBy(e => e.Sort).ThenBy(e => e.Id).ToList().Select(p => ToDTO(p)).ToArray();
        }

        #region 递归获取菜单
        /// <summary>
        /// 递归获取菜单
        /// </summary>
        /// <param name="navLevel">菜单级数， 不指定，默认返回全部</param>
        /// <returns></returns>
        public List<SiteNavDTO> GetSiteNavsByLevel(int navLevel = 0)
        {
            SiteNavDTO[] allSiteNav = GetSiteNavs();
            return GetGetSiteNavs(allSiteNav, 0, navLevel);
        }

        /// <summary>
        /// 递归获取菜单
        /// </summary>
        /// <param name="allSiteNav"></param>
        /// <param name="parentId"></param>
        /// <param name="navLevel"></param>
        /// <returns></returns>
        private List<SiteNavDTO> GetGetSiteNavs(SiteNavDTO[] allSiteNav, long parentId, int navLevel)
        {
            List<SiteNavDTO> list = new List<SiteNavDTO>();
            //默认获取全部菜单
            IEnumerable<SiteNavDTO> childSiteNavs = allSiteNav.Where(e => e.ParentId == parentId);
            if (navLevel != 0)
            {
                //获取指定级数菜单
                childSiteNavs = childSiteNavs.Where(e => e.NavLevel <= navLevel);
            }

            //没有子菜单
            if (childSiteNavs.Count() <= 0)
            {
                return list;
            }

            foreach (SiteNavDTO dto in childSiteNavs)
            {
                list.Add(dto);
                //递归获取子菜单
                list.AddRange(GetGetSiteNavs(allSiteNav, dto.Id, navLevel));
            }

            return list;
        }
        #endregion

        public SiteNavDTO GetDTOById(long id)
        {
            SiteNav entity = GetById(id);
            if (entity == null)
            {
                return null;
                //throw new Exception("获取不到菜单(Id:" + id + ")信息");
            }
            return ToDTO(entity);
        }

        public SiteNavDTO[] GetSiteNavsByFunCode(string funCode)
        {
            return GetJoinAll().Where(e=>e.FunctionalModule.FunCode== funCode).ToList().Select(e=> ToDTO(e)).ToArray();
        }

        private SiteNavDTO ToDTO(SiteNav entity)
        {
            SiteNavDTO dto = new SiteNavDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.FunctionalModuleId = entity.FunctionalModuleId;
            dto.ParentId = entity.ParentId;
            dto.NavLevel = entity.NavLevel;
            dto.NavName = entity.NavName;
            dto.NavTarget = entity.NavTarget;
            dto.NavUrl = entity.NavUrl;
            dto.NavImg = entity.NavImg;
            //生成静态页模板
            dto.ViewHtmlTemplate = entity.FunctionalModule.ViewHtmlTemplate;
            if (!string.IsNullOrEmpty(entity.ViewHtmlTemplate))
            {
                dto.ViewHtmlTemplate = entity.ViewHtmlTemplate;
            }
            dto.ListHtmlTemplate = entity.FunctionalModule.ListHtmlTemplate;
            if (!string.IsNullOrEmpty(entity.ListHtmlTemplate))
            {
                dto.ListHtmlTemplate = entity.ListHtmlTemplate;
            }
            dto.Sort = entity.Sort;
            dto.IsShow = entity.IsShow;
            dto.IsEnabled = entity.IsEnabled;
            dto.Remark = entity.Remark;

            dto.FunName = entity.FunctionalModule.FunName;
            dto.FunCode = entity.FunctionalModule.FunCode;
            dto.CtrlUrl = entity.FunctionalModule.CtrlUrl;
            //内页 Banner 图片
            dto.BannerImg = "/upload/images/BannerInner.jpg";
            if (!string.IsNullOrEmpty(entity.NavImg) && !entity.NavImg.Contains("noPic.jpg"))
            {
                dto.BannerImg = entity.NavImg;
            }
            return dto;
        }

        public SiteNav Add(FunctionalModuleDTO modelFunctionalModule, SiteNavDTO modelParentNav, string navName, string navUrl, string navTarget, bool isShow, string navImg, int sort)
        {

            SiteNav entity = new SiteNav();
            entity.FunctionalModuleId = modelFunctionalModule.Id;
            if (modelParentNav == null)
            {
                entity.ParentId = 0;
                entity.NavLevel = 1;
            }
            else
            {
                entity.ParentId = modelParentNav.Id;
                entity.NavLevel = modelParentNav.NavLevel + 1;
            }
            entity.NavName = navName;
            entity.NavTarget = navTarget;
            entity.NavImg = navImg;
            entity.Sort = sort;
            entity.IsShow = isShow;
            entity.IsEnabled = true;
            //获取新增菜单
            entity = Add(entity);



            #region 更新菜单链接
            if (modelFunctionalModule.FunCode == "Info")
            {
                SiteContent entitySiteContent = new SiteContent();
                entitySiteContent.SiteNavId = entity.Id;
                entitySiteContent.Title = navName;
                entitySiteContent.SubmitDate = DateTime.Now;
                entitySiteContent.Sort = 0;
                entitySiteContent.IsEnabled = true;
                entitySiteContent.IsShow = true;
                entitySiteContent.ImgLG = entity.NavImg;
                //新增
                entitySiteContent = siteContentService.Add(entitySiteContent);
                entitySiteContent.LinksUrl = "/" + modelFunctionalModule.FunCode + "/" + entitySiteContent.Id + ".html";
                //修改
                siteContentService.Update(entitySiteContent);

                entity.NavUrl = "/" + modelFunctionalModule.FunCode + "/" + entitySiteContent.Id + ".html";
            }
            else if (modelFunctionalModule.FunCode == "Other")
            {
                entity.NavUrl = navUrl;
            }
            else
            {
                entity.NavUrl = "/" + modelFunctionalModule.FunCode + "/" + entity.Id + "/list-1.html";
            }
            Update(entity);
            #endregion

            return entity;

        }
        public void Update(long id, FunctionalModuleDTO modelFunctionalModule, SiteNavDTO modelParentNav, string navName, string navUrl, string navTarget, bool isShow, string navImg, int sort)
        {
            SiteNav entity = GetById(id);
            if (entity == null)
            {
                throw new Exception("获取不到菜单(Id:" + id + ")信息");
            }
            entity.FunctionalModuleId = modelFunctionalModule.Id;
            if (modelParentNav == null)
            {
                entity.ParentId = 0;
                entity.NavLevel = 1;
            }
            else
            {
                entity.ParentId = modelParentNav.Id;
                entity.NavLevel = modelParentNav.NavLevel + 1;
            }
            entity.NavName = navName;
            entity.NavTarget = navTarget;
            entity.NavImg = navImg;
            entity.Sort = sort;
            entity.IsShow = isShow;
            Update(entity);

            #region 更新菜单链接
            if (modelFunctionalModule.FunCode == "Info")
            {
                //介绍页
                SiteContent entitySiteContent = siteContentService.GetInfoByNavId(id);
                if (entitySiteContent == null)
                {
                    entitySiteContent = new SiteContent();
                    entitySiteContent.SiteNavId = entity.Id;
                    entitySiteContent.Title = navName;
                    entitySiteContent.SubmitDate = DateTime.Now;
                    entitySiteContent.Sort = 0;
                    entitySiteContent.IsEnabled = true;
                    entitySiteContent.IsShow = true;
                    entitySiteContent.ImgLG = entity.NavImg;
                    //新增
                    entitySiteContent = siteContentService.Add(entitySiteContent);
                    entitySiteContent.LinksUrl = "/" + modelFunctionalModule.FunCode + "/" + entitySiteContent.Id + ".html";
                    //修改
                    siteContentService.Update(entitySiteContent);
                }
                else
                {
                    entitySiteContent.ImgLG = entity.NavImg;
                    entitySiteContent.SubmitDate = DateTime.Now;
                    entitySiteContent.Title = navName;
                    entitySiteContent.IsDeleted = false;
                    //修改
                    siteContentService.Update(entitySiteContent);
                }

                entity.NavUrl = "/" + modelFunctionalModule.FunCode + "/" + entitySiteContent.Id + ".html";
            }
            else if (modelFunctionalModule.FunCode == "Other")
            {
                //其他链接
                entity.NavUrl = navUrl;
            }
            else
            {
                entity.NavUrl = "/" + modelFunctionalModule.FunCode + "/" + entity.Id + "/list-1.html";
            }
            Update(entity);


            //删除介绍页内容
            if (modelFunctionalModule.FunCode != "Info")
            {
                //介绍页
                SiteContent entitySiteContent = siteContentService.GetInfoByNavId(id);
                if (entitySiteContent != null)
                {
                    entitySiteContent.IsDeleted = true;
                    siteContentService.Update(entitySiteContent);
                }
            }

            #endregion
        }
    }
}
